using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PicturePage
{
    public partial class TemplateSettings : Form
    {
        private Configuration config;

        public TemplateSettings(Configuration configuration)
        {
            InitializeComponent();

            config = configuration;
            this.txtTemplateFile.Text = config.TemplateFile;
            this.txtInsert.Text = config.TemplateInsertLocation;

            if (this.txtTemplateFile.Text != "")
            {
                this.btnFind.Enabled = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!File.Exists(this.txtTemplateFile.Text))
            {
                if (MessageBox.Show("The template file does not exist. Do you still want to save?", "Sure you want to save?", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    this.Close();
                    return;
                }
            }

            if (CheckFileInsertText())
            {
                config.TemplateFile = this.txtTemplateFile.Text;
                config.TemplateInsertLocation = this.txtInsert.Text;

                Configuration.Serialize("config.xml", config);
                this.Close();
            }
            else
            {
                MessageBox.Show("Settings were not saved!");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        /// <summary>
        /// Event that occurs when the browse button is clicked
        /// </summary>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "HTML, PHP, ASP, ASPX|*.html;*.php;*.asp;*.aspx";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtTemplateFile.Text = openFileDialog1.FileName;
                this.btnFind.Enabled = true;
            }
        }

        /// <summary>
        /// Allows the user to find the text in the template file selected
        /// </summary>
        private void btnFind_Click(object sender, EventArgs e)
        {
            //place to insert pictures << allow to open file and select text
            if (File.Exists(this.txtTemplateFile.Text))
            {
                TemplateFind find = new TemplateFind(this.txtTemplateFile.Text, this.txtInsert);
                find.ShowDialog();
            }
            else
            {
                MessageBox.Show("Template file does not exist!");
            }
        }

        /// <summary>
        /// Checks to see if the text is in the file if it is there
        /// </summary>
        /// <returns>false if the text is not there and true if it is there or if the file does not exist</returns>
        private bool CheckFileInsertText()
        {
            //when leaving check to see if this string is in the template file
            if (File.Exists(this.txtTemplateFile.Text))
            {
                if (!File.ReadAllText(this.txtTemplateFile.Text).Contains(this.txtInsert.Text))
                {
                    MessageBox.Show("This text does not exist in the template file.");
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Button allows you to check to see if the entered text is in the template file
        /// </summary>
        private void btnCheck_Click(object sender, EventArgs e)
        {
            //when leaving check to see if this string is in the template file
            if (File.Exists(this.txtTemplateFile.Text))
            {
                if (!File.ReadAllText(this.txtTemplateFile.Text).Contains(this.txtInsert.Text))
                {
                    MessageBox.Show("This text does not exist in the template file.");

                }
                else
                {
                    MessageBox.Show("Text was found.");
                }
            }
            else
            {
                MessageBox.Show("Template file does not exist.");
            }
        }

        /// <summary>
        /// As the text changes if there is anything then then enable the File button
        /// </summary>
        private void txtTemplateFile_TextChanged(object sender, EventArgs e)
        {
            if (this.txtTemplateFile.Text == "")
            {
                this.btnFind.Enabled = false;
            }
            else
            {
                this.btnFind.Enabled = true;
            }
        }
    }
}